package com.company.chatRoom;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther Xue
 * @Date 2021/5/10
 * @Version 1.0
 * @Desc
 */
public class Utils {
    private static Map<Socket,ObjectOutputStream> oos = new HashMap<>();
    private static Map<Socket, ObjectInputStream> ois = new HashMap<>();
    public static ObjectOutputStream getOOSBySocket(Socket socket) throws IOException {
        if(oos.get(socket)!=null){
            return  oos.get(socket);
        }
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
        oos.put(socket,objectOutputStream);
        return objectOutputStream;
    }

    public static ObjectInputStream getOISBySocket(Socket socket) throws IOException {
        if(ois.get(socket)!=null){
            return  ois.get(socket);
        }
        ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
        ois.put(socket,in);
        return in;
    }
    public static Boolean isServerClose(Socket socket){
        try{
//            socket.sendUrgentData(0xFF);//发送1个字节的紧急数据，默认情况下，服务器端没有开启紧急数据处理，不影响正常通信
            socket.getInputStream();
            return false;
        }catch(Exception se){
            return true;
        }
    }
}
